package main

import (
	"context"

	"gitee.com/zawei/yggdrasil"
	"gitee.com/zawei/yggdrasil/example/api"
	v1 "gitee.com/zawei/yggdrasil/example/api/v1"
	"gitee.com/zawei/yggdrasil/pkg/app"
	"gitee.com/zawei/yggdrasil/pkg/cmd"
	"gitee.com/zawei/yggdrasil/pkg/errors"
	"gitee.com/zawei/yggdrasil/pkg/log"
	"gitee.com/zawei/yggdrasil/pkg/metrics"
	"gitee.com/zawei/yggdrasil/pkg/metrics/prometheus"
	"gitee.com/zawei/yggdrasil/pkg/registry/nacos"
	"gitee.com/zawei/yggdrasil/pkg/transport/server/gin"
	"gitee.com/zawei/yggdrasil/pkg/transport/server/grpc"
	"gitee.com/zawei/yggdrasil/third_party/urfave/cli"
)

// server is used to implement helloworld.GreeterServer.
type Server struct {
	v1.UnimplementedGreeterServer
}

// SayHello implements helloworld.GreeterServer
func (s *Server) SayHello(ctx context.Context, in *v1.HelloRequest) (*v1.HelloReply, error) {
	if in.Name == "ok" {
		return &v1.HelloReply{Message: "Hello " + in.GetName()}, nil
	} else {
		return nil, errors.InvalidArgument("invalid argument: %s", in.Name).WithErrInfo(api.Errors_TTD, "fd", "fd").Err()
	}
}

func main() {
	grpc.RegisterService(&v1.Greeter_ServiceDesc, &Server{})
	gin.RegisterService(&v1.Local_Greeter_handlerDesc, &Server{})
	cmd.WithOptions(cmd.Action(func(c *cli.Context) error {
		return app.New(
			app.WithRegistry(nacos.NewRegistry()),
		).Run()
	}))
	metrics.SetGlobalRegistry(prometheus.NewMetricsRegistry())
	if err := yggdrasil.Run(); err != nil {
		log.Fatalf(err.Error())
	}
}
